var imgData;
let i, addition, addition1, addition2;
import * as noise_gen from "./noise_generators.js"
import * as offsets from "./offset_filters";
import * as colour_tints from "./colour_tints"
import * as line_gen from "./line_generators"
import * as specks_gen from "./specks_generators"
import * as brightness_adj from "./brightness_adjs"

function pixelsJS() {

  return {

    // Browser-only function
    filterImg: (img, filter,param={width:-1,height:-1}) => {
      let filterExists = checkIfFilterExists(filter);
      if (filterExists) {
        let filter_dict = getFilterNames();
        let canvas_and_context = imgToCanvas(img,param);
        let canvas = canvas_and_context[0]
        let context = canvas_and_context[1]

        imgData = context.getImageData(0, 0, canvas.width, canvas.height);
        let new_img_data = filter_dict[filter](imgData);

        context.putImageData(imgData, 0, 0)

        img.parentNode.replaceChild(canvas, img);
      }

      else {
        //如果没有滤镜则直接替换
        let canvas_and_context = imgToCanvas(img,param);
        let canvas = canvas_and_context[0]
        img.parentNode.replaceChild(canvas, img);
        //   console.log()
        console.log(`PixelsJS ERROR: Filter ${filter} does not exist`)
      }
    },

    // Browser and NodeJS compatible
    filterImgData: (user_img_data, filter) => {
      let filterExists = checkIfFilterExists(filter);

      if (filterExists) {
        // Re-initialize the data in the imgData object
        let filter_dict = getFilterNames();

        imgData = user_img_data;
        let new_img_data = filter_dict[filter](imgData);
        return new_img_data;
      }

      else {
        console.log(`PixelsJS Error: Filter ${filter} does not exist`)
      }

    },

    getFilterList: () => {
      let filter_dict = getFilterNames();
      let filters = Object.keys(filter_dict);
      return filters;
    }
  };
}

const getFilterNames = () => {
  let filter_dict = { "horizontal_lines": line_gen.add_horizontal_line_imgdata, "extreme_offset_blue": offsets.extreme_offset_blue, "ocean": colour_tints.ocean_imgdata, "extreme_offset_green": offsets.extreme_offset_green, "offset_green": offsets.offset_green_imgdata, "extra_offset_blue": offsets.extra_offset_blue, "extra_offset_red": offsets.extra_offset_red, "extra_offset_green": offsets.extra_offset_green, "extreme_offset_red": offsets.extreme_offset_red, "specks_redscale": specks_gen.specksredscale_imgdata, "vintage": colour_tints.vintage_imgdata, "perfume": colour_tints.perfume_imgdata, "serenity": colour_tints.serenity_imgdata, "eclectic": specks_gen.eclectic_imgdata, "diagonal_lines": line_gen.add_diagonal_lines_imgdata, "green_specks": specks_gen.green_specks_imgdata, "warmth": colour_tints.warmth, "casino": specks_gen.casino_imgdata, "green_diagonal_lines": line_gen.add_green_diagonal_lines_imgdata, "offset": offsets.offset, "offset_blue": offsets.offset_blue_imgdata, "neue": colour_tints.neue_imgdata, "sunset": colour_tints.sunset, "specks": specks_gen.specks_imgdata, "wood": colour_tints.wood, "lix": colour_tints.lix_conv, "ryo": colour_tints.ryo_conv, "bluescale": colour_tints.blue_greyscale_imgdata, "solange": colour_tints.solange_imgdata, "evening": colour_tints.evening_imgdata, "crimson": colour_tints.crimson, "teal_min_noise": noise_gen.teal_min_noise_imgdata, "phase": colour_tints.phase, "dark_purple_min_noise": noise_gen.dark_purple_min_noise_imgdata, "coral": colour_tints.coral_imgdata, "darkify": brightness_adj.darkify_imgdata, "incbrightness": brightness_adj.incbrightness_imgdata, "incbrightness2": brightness_adj.incbrightness_two_imgdata, "yellow_casino": specks_gen.yellow_casino_imgdata, "invert": brightness_adj.invert_imgdata, "sat_adj": brightness_adj.sat_adj_imgdata, "lemon": colour_tints.lemon_imgdata, "pink_min_noise": noise_gen.pink_min_noise_imgdata, "frontward": colour_tints.frontward_imgdata, "pink_aura": colour_tints.pink_aura_imgdata, "haze": colour_tints.haze_imgdata, "cool_twilight": colour_tints.cool_twilight_imgdata, "blues": colour_tints.blues_imgdata, "horizon": colour_tints.horizon_imgdata, "mellow": colour_tints.mellow_imgdata, "solange_dark": colour_tints.solange_dark_imgdata, "solange_grey": colour_tints.solange_grey_imgdata, "zapt": colour_tints.zapt_imgdata, "eon": colour_tints.eon_imgdata, "aeon": colour_tints.aeon_imgdata, "matrix": noise_gen.matrix_imgdata, "cosmic": noise_gen.cosmic_imgdata, "min_noise": noise_gen.min_noise_imgdata, "red_min_noise": noise_gen.red_min_noise_imgdata, "matrix2": noise_gen.matrix2, "purplescale": colour_tints.purplescale_imgdata, "radio": colour_tints.radio_imgdata, "twenties": colour_tints.twenties_imgdata, "a": brightness_adj.a, "pixel_blue": brightness_adj.pixel_blue_imgdata, "greyscale": colour_tints.greyscale_imgdata, "grime": colour_tints.grime, "redgreyscale": colour_tints.redgreyscale_imgdata, "retroviolet": specks_gen.retroviolet_imgdata, "greengreyscale": colour_tints.greengreyscale_imgdata, "crimson": colour_tints.crimson, "green_med_noise": noise_gen.green_med_noise_imgdata, "green_min_noise": noise_gen.green_min_noise_imgdata, "blue_min_noise": noise_gen.blue_min_noise_imgdata, "rosetint": colour_tints.rosetint_imgdata, "purple_min_noise": noise_gen.purple_min_noise_imgdata }
  return filter_dict;
}

const checkIfFilterExists = (filter) => {
  let filter_dict = getFilterNames();
  let filters = Object.keys(filter_dict)
  if (filters.includes(filter)) {
    return true
  }
  return false
}

const imgToCanvas = (img,param) => {
  var canvas = document.createElement("canvas");
 
  let imgWidth = param.width || img.naturalWidth;
  let imgHeight = param.height || img.naturalHeight;
  console.log(imgWidth,imgHeight)
  canvas.height = imgHeight;
  canvas.width = imgWidth;

  var context = canvas.getContext("2d");
  var ptrn = context.createPattern(img, 'no-repeat');
  context.fillStyle = ptrn;
  context.fillRect(0, 0, canvas.width, canvas.height);

  return [canvas, context];
}

const getRandomNumber = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

const checkIfBrowser = () => {
  module.exports = false;
  try {
    module.exports = Object.prototype.toString.call(global.process) === '[object process]'
    console.log("Cannot use browser functions in Node!");

  }
  catch (e) {
    return true
  }
  return false
}


export default pixelsJS();
